package android.dj.lbscamera.activities;

import org.json.JSONException;
import org.json.JSONObject;

import com.mobclick.android.MobclickAgent;

import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.dj.lbscamera.R;
import android.dj.lbscamera.network.ResourceManager;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;
import android.view.KeyEvent;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class PageFeedback extends BaseActivity {
	protected BaseApplication app;
	private EditText mMessage;
	private ProgressDialog mDialog;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.feedback);
		app = (BaseApplication) getApplication();
		mDialog = new ProgressDialog(this);
		mMessage = (EditText) findViewById(R.id.message);
		((Button) findViewById(R.id.type))
				.setOnClickListener(new OnClickListener() {

					public void onClick(View v) {
						// TODO
					}
				});

		((Button) findViewById(R.id.feed))
				.setOnClickListener(new OnClickListener() {

					public void onClick(View v) {
						if(mDialog != null && !mDialog.isShowing()){
							mDialog.show();
						}
						new userCommentAsync().execute(mMessage.getText().toString());
					}
				});
	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_BACK) {
			PageFeedback.this.finish();
			return true;
		}
		return super.onKeyDown(keyCode, event);
	}

	@Override
	protected void onResume() {
		super.onResume();
		MobclickAgent.onResume(PageFeedback.this);
	}

	@Override
	protected void onPause() {
		super.onPause();
		MobclickAgent.onPause(PageFeedback.this);
	}

	public class userCommentAsync extends AsyncTask<String, Void, String> {

		@Override
		protected String doInBackground(String... params) {
			String result = "";
			try {
				result = ResourceManager.getInstance().doComment(
						app.user.getUid(), params[0]);
			} catch (Exception e) {
				e.printStackTrace();
			}
			return result;
		}

		@Override
		protected void onPostExecute(String result) {
			if(!TextUtils.isEmpty(result)){
				JSONObject json;
				try {
					json = new JSONObject(result);
					String mResult = json.getString("result");
					if(!TextUtils.isEmpty(mResult)){
						switch (Integer.valueOf(mResult)) {
						case 0://success
							Toast.makeText(PageFeedback.this, getString(R.string.thank), Toast.LENGTH_SHORT).show();
							break;
						case 1://failed
							Toast.makeText(PageFeedback.this, getString(R.string.comment_fail), Toast.LENGTH_SHORT).show();
							break;
						default:
							break;
						}
					}
				} catch (JSONException e) {
					e.printStackTrace();
				}
			}
			if(mDialog != null && mDialog.isShowing()){
				mDialog.dismiss();
			}
			super.onPostExecute(result);
		}

	}

}
